//package com.example.dmweb.chat.chatService;
//
//import com.alibaba.fastjson.JSON;
//import com.example.dmcommom.HttpUtil;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import lombok.extern.slf4j.Slf4j;
//import okhttp3.OkHttpClient;
//import okhttp3.Request;
//import okhttp3.Response;
//import okhttp3.ResponseBody;
//import okio.BufferedSource;
//
//import java.io.IOException;
//import java.util.concurrent.TimeUnit;
//
//@Slf4j
//public class GPTClient {
//    private final OkHttpClient client;
//
//    private final String model = "gpt-3.5-turbo";
//
//
//    public GPTClient(OkHttpClient client) {
//        this.client = client;
//    }
//
//
//    private OkHttpClient createOkHttpClient() {
//        return new OkHttpClient.Builder()
//                .readTimeout(60, TimeUnit.SECONDS)
//                .build();
//    }
//
//    public String streamChat(String message) {
//        return new HttpUtil().okHttpPost(
//                "https://serverfastgpt.excn.top/api/v1/chat/completions"
//                , "{\"model\": \"" + model + "\", \"messages\": [{\"role\": \"user\", \"content\": \"" + message + "\"}], \"stream\": true}");
//
//    }
//
//    public void streamChat(ChatRequest chatRequest, StreamListener listener) {
//        okhttp3.MediaType mediaType = okhttp3.MediaType.get("application/json");
//        okhttp3.RequestBody body = okhttp3.RequestBody.create(mediaType, JSON.toJSONString(chatRequest));
//        Request request = new Request.Builder()
//                .url("http://112.111.7.68:13201/ele_bill")
//                .header("Authorization", "Basic YmFja2VuZF9hY2NvdW50OmJhY2tlbmRfMjAyNDA1MzA=")
//                .addHeader("Accept", "text/event-stream")
//                //                .addHeader("Content-Type", "application/json; charset=UTF-8")
//                .post(body)
//                .build();
//        new Thread(() -> {
//            try (Response response = OkHttpClient.newCall(request).execute()) {
//                if (!response.isSuccessful())
//                    throw new IOException("Request failed with HTTP error code: " + response.code());
//
//                ResponseBody responseBody = response.body();
//                if (responseBody == null) return;
//
//                try (BufferedSource source = responseBody.source()) {
//                    while (!source.exhausted()) {
//                        String data = source.readUtf8Line();
//                        if (data != null) {
//                            listener.onData(data);
//                        }
//                    }
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }).start();
//    }
//
//
//    interface StreamListener {
//        void onData(String data) throws JsonProcessingException;
//    }
//}
